/**
 * Maybe函子
 */
class Maybe {
  static of (value) {
    return new Maybe(value)
  }

  constructor(value) {
    this._value = value
  }
  // 判断value是不是null或undefined
  isNothing() {
    return this._value == null || this._value === undefined
  }

  map (fn) {
    // 如果value是null或undefined，就原样返回函子，不是就执行fn并返回新的函子
    return this.isNothing() ? Maybe.of(this._value) : Maybe.of(fn(this._value))
  }
}

/**
 * functor
 */
class Container {
  static of (value) {
    return new Container(value)
  }

  constructor(value) {
    this._value = value
  }

  map(fn) {
    return Container.of(fn(this._value))
  }
}

module.exports = {
  Maybe,
  Container
}
